﻿Shader "URP/Desert"
{
    Properties
    {
        _SmoothAll("光滑度", Range(0, 1)) = 0.7
        _MetallicAll("金属度", Range(0, 1)) = 0
        _Occlusion("环境光", Range(0, 10)) = 1
        [Header(Animation)]
        [Space]
        [HDR] _AnimationColorTint("动画颜色", Color) = (1, 1, 1, 1)
        [NoScaleOffset]_AnimationTexture("动画贴图", 2D) = "white" {}
        _Col("列", Float) = 1
        _Row("行", Float) = 1
        _Speed("动画速度", Float) = 1
        _MoveSpeedX("移动速度X", Float) = 1
        _MoveSpeedY("移动速度Y", Float) = 1
        _Scale("缩放", Float) = 1
        _Density("密度", Range(0, 1)) = 0
        _Shape("分布", Vector) = (12.9898, 78.233, 1, 1)
    }

    SubShader
    {
        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }
        Blend SrcAlpha OneMinusSrcAlpha

        LOD 200

        CGPROGRAM

        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows alpha

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _AnimationTexture;

        struct Input
        {
            float2 uv_AnimationTexture;
        };

        half _SmoothAll;
        half _Metallic;
        half _Occlusion;

        float4 _AnimationColorTint;
        float _Col;
        float _Row;
        float _Speed;
        float _Scale;
        float _MoveSpeedX;
        float _MoveSpeedY;
        float _Density;
        float4 _Shape;

        float2 Unity_Flipbook_float(float2 UV, float Width, float Height, float Tile)
        {
            half2 uvSize = half2(1.0 / (int)Width, 1.0 / (int)Height);
            half2 uvItem = UV * uvSize;
            uvItem.x += uvSize.x * (int)Tile;
            uvItem.y += uvSize.y * -(int)((Tile + Width) * uvSize.x);
            return uvItem;
        }

        inline float Unity_SimpleNoise_RandomValue_float(float2 uv)
        {
            return frac(sin(dot(uv, float2(12.9898, 78.233))) * 43758.5453);
        }

        inline float Unity_SimpleNnoise_Interpolate_float(float a, float b, float t)
        {
            return (1.0 - t) * a + (t * b);
        }

        inline float Unity_SimpleNoise_ValueNoise_float(float2 uv)
        {
            float2 i = floor(uv);
            float2 f = frac(uv);
            f = f * f * (3.0 - 2.0 * f);

            uv = abs(frac(uv) - 0.5);
            float2 c0 = i + float2(0.0, 0.0);
            float2 c1 = i + float2(1.0, 0.0);
            float2 c2 = i + float2(0.0, 1.0);
            float2 c3 = i + float2(1.0, 1.0);
            float r0 = Unity_SimpleNoise_RandomValue_float(c0);
            float r1 = Unity_SimpleNoise_RandomValue_float(c1);
            float r2 = Unity_SimpleNoise_RandomValue_float(c2);
            float r3 = Unity_SimpleNoise_RandomValue_float(c3);

            float bottomOfGrid = Unity_SimpleNnoise_Interpolate_float(r0, r1, f.x);
            float topOfGrid = Unity_SimpleNnoise_Interpolate_float(r2, r3, f.x);
            float t = Unity_SimpleNnoise_Interpolate_float(bottomOfGrid, topOfGrid, f.y);
            return t;
        }

        float Unity_SimpleNoise_float(float2 UV)
        {
            float t = 0.0;

            float freq = pow(2.0, float(0));
            float amp = pow(0.5, float(3 - 0));
            t += Unity_SimpleNoise_ValueNoise_float(float2(UV.x / freq, UV.y / freq)) * amp;

            freq = pow(2.0, float(1));
            amp = pow(0.5, float(3 - 1));
            t += Unity_SimpleNoise_ValueNoise_float(float2(UV.x / freq, UV.y / freq)) * amp;

            freq = pow(2.0, float(2));
            amp = pow(0.5, float(3 - 2));
            t += Unity_SimpleNoise_ValueNoise_float(float2(UV.x / freq, UV.y / freq)) * amp;

            return t;
        }

        float4 GetAnimationColor(float2 uv)
        {
            float2 diyUV = uv * _Scale + _Time.y * float2(_MoveSpeedX, _MoveSpeedY);

            float noise = Unity_SimpleNoise_float(floor(diyUV));
            noise = floor(noise * sin(floor(diyUV.x)) * sin(floor(diyUV.y)) + _Density);

            float2 AnimationUV = frac(diyUV);
            AnimationUV = Unity_Flipbook_float(AnimationUV, _Col, _Row, ceil(_Speed * _Time.y));
            AnimationUV = frac(AnimationUV);
            float4 AnimationColor = tex2D(_AnimationTexture, AnimationUV);

            AnimationColor.a *= noise;

            return AnimationColor;
        }

        void surf(Input IN, inout SurfaceOutputStandard o)
        {
            float4 AnimationColor = GetAnimationColor(IN.uv_AnimationTexture);
            o.Albedo = AnimationColor.rgb * _AnimationColorTint.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _SmoothAll;
            o.Alpha = AnimationColor.a;

            clip(AnimationColor.a - _AnimationColorTint.a);
        }
        ENDCG
    }
}
